package com.bkgy.modular.tbdatadictionary.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.tbdatadictionary.dao.TbDataDictionaryDao;
import com.bkgy.modular.tbdatadictionary.entity.JTbDataDictionary;
import com.bkgy.modular.tbdatadictionary.entity.TbDataDictionary;
import com.bkgy.modular.tbdatadictionary.service.TbDataDictionaryService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* 数据字典1接口具体实现类
* @author  生成器生成
* @date 2022-07-20 17:18:27
*/
@Service
public class TbDataDictionaryServiceImpl implements TbDataDictionaryService {
	
	@Autowired
	private TbDataDictionaryDao tbDataDictionaryDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbDataDictionary(String data, Long userId, String sname){
		try{
			JTbDataDictionary jtbDataDictionary = JSON.parseObject(data,JTbDataDictionary.class);
    		TbDataDictionary tbDataDictionary = jtbDataDictionary.getTbDataDictionary();
    		CodiUtil.newRecord(userId,sname,tbDataDictionary);
            tbDataDictionaryDao.insertDataTbDataDictionary(tbDataDictionary);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbDataDictionaryOne(Long indocno){
		try {
            tbDataDictionaryDao.deleteDataTbDataDictionaryOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDataDictionaryMany(String str_id) {
        try {
        	String sql = "delete tb_data_dictionary where id in(" + str_id +")";
            tbDataDictionaryDao.deleteDataTbDataDictionaryMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbDataDictionary(String data,Long userId,String sname){
		try {
			JTbDataDictionary jtbDataDictionary = JSON.parseObject(data,JTbDataDictionary.class);
    		TbDataDictionary tbDataDictionary = jtbDataDictionary.getTbDataDictionary();
        	CodiUtil.editRecord(userId,sname,tbDataDictionary);
            tbDataDictionaryDao.updateDataTbDataDictionary(tbDataDictionary);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDataDictionaryByPage(String data) {
        try {
        	JTbDataDictionary jtbDataDictionary = JSON.parseObject(data, JTbDataDictionary.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtbDataDictionary.getPageIndex();
        	Integer pageSize = jtbDataDictionary.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtbDataDictionary.getCondition()){
    			jsonObject  = JSON.parseObject(jtbDataDictionary.getCondition().toString());
    		}


			String name = null;
			if (!StringUtils.isEmpty(jsonObject.get("name"))) {
				name = jsonObject.get("name").toString();
			}

			String device_no = null;
			if (!StringUtils.isEmpty(jsonObject.get("device_no"))) {
				device_no = jsonObject.get("device_no").toString();
			}


    		List<TbDataDictionary> list = tbDataDictionaryDao.findDataTbDataDictionaryByPage((pageIndex-1)*pageSize, pageIndex*pageSize,name,device_no);
    		Integer count = tbDataDictionaryDao.findDataTbDataDictionaryByPageSize(name,device_no);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDataDictionaryByIndocno(String data) {
        try {
        	JTbDataDictionary jtbDataDictionary = JSON.parseObject(data, JTbDataDictionary.class); 
        	//Long indocno = jtbDataDictionary.getIndocno();
			String indocno = jtbDataDictionary.getStr_indocno();
    		TbDataDictionary tbDataDictionary = tbDataDictionaryDao.findDataTbDataDictionaryByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbDataDictionary);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbDataDictionary> findDataTbDataDictionary(){
		List<TbDataDictionary> list = tbDataDictionaryDao.findDataTbDataDictionary();
		return list;
	};
}
